
const { uploadFile } = require('../api/index')
module.exports = {
    async upload (ctx) {
        let key = ctx.request.files.file.name;
        let localFile = '/tmp/' + key;
        let argv = { ...ctx.query, ...ctx.body, key, localFile }
        await uploadFile.upload(argv).then(function (data) {
            console.log(data, 'success');
            ctx.body = {
                success: true,
                data: data,
                name: key,
                message: '操作成功',
                code: 200
            }
        }).catch(function (err) {
            console.log(err, 'error');
            ctx.body = {
                success: false,
                message: (err.error && err.error.status) || '服务器异常',
                data: null,
                code: 200
            }
        })
        return ctx.body
    },
    async getFiles (ctx) {
        let argv = { ...ctx.query }
        await uploadFile.getFiles(argv).then(function (data) {
            ctx.body = {
                success: true,
                data,
                message: '操作成功',
                code: 200
            }
        }).catch(function (err) {
            console.log(err, 'error');
            ctx.body = {
                success: false,
                message: (err.error && err.error.status) || '服务器异常',
                data: null,
                code: 200
            }
        })
        return ctx.body
    },
    async deleteFiles (ctx) {
        let argv = { ...ctx.request.body }
        if (!argv.ids) {
            ctx.body = {
                success: true,
                data: null,
                message: '参数:ids不能为空',
                code: 999
            }
            return ctx.body
        }
        await uploadFile.deleteFiles(argv).then(function (data) {
            ctx.body = {
                success: true,
                data,
                message: '操作成功',
                code: 200
            }
        }).catch(function (err) {
            console.log(err, 'error');
            ctx.body = {
                success: false,
                message: (err.error && err.error.status) || '服务器异常',
                data: null,
                code: 200
            }
        })
        return ctx.body
    },
    async updateFiles (ctx) {
        let argv = { ...ctx.request.body }
        if (!argv.id || !argv.name || !argv.url) {
            ctx.body = {
                success: true,
                data: null,
                message: '参数不能为空',
                code: 999
            }
            return ctx.body
        }
        await uploadFile.updateFiles(argv).then(function (data) {
            ctx.body = {
                success: true,
                data,
                message: '操作成功',
                code: 200
            }
        }).catch(function (err) {
            console.log(err, 'error');
            ctx.body = {
                success: false,
                message: (err.error && err.error.status) || '服务器异常',
                data: null,
                code: 200
            }
        })
        return ctx.body
    }
}